<template>
     <view>
          <view v-if="pageLoading" style="padding-top: 44px;">
               <ant-skeleton />
          </view>
          <template v-else>
               <define-template ref="define" v-if="template_type==1">
               </define-template>
               <diy-template :scrollTop="scrollTop" ref="diy" v-if="template_type==2">
               </diy-template>
          </template>
     </view>
</template>
<script>
var app = getApp().globalData
import defineTemplate from "./components/define_template/define_template.vue";
import diyTemplate from "./components/diy_template/diy_template.vue";
export default {
     components: {
          defineTemplate,
          diyTemplate
     },
     data () {
          return {
               template_type: 1, // 1是默认首页 2是diy装修
               pageLoading: false, // 页面加载loading
          }

     },
     onShow () {
          if(this.template_type === 1){
               this.$nextTick(() => {
                    if(this.$refs.define){
                         this.$refs.define.$nextTick(() => {
                              this.$refs.define.getDataList()
                         });
                    }
               });
          }
      
          //获取子组件方法
          this.$nextTick(() => {
               if (this.$refs.diy) {
                    this.$refs.diy.$nextTick(() => {
                         this.$refs.diy.doSomething()
                    });
               }
          });
     },
     async onLoad () {
          await this.$onLaunched;
          await this.checkTemplateType();
          // #ifdef MP-WEIXIN
          uni.showShareMenu({
               menus: ['shareAppMessage', 'shareTimeline']
          });
          // #endif
     },

     /**
      * 上拉刷新加载
      */
     async onPullDownRefresh () {
          this.$nextTick(() => {
               let template_arr = [
                    "define", "diy"
               ];
               this.$refs[template_arr[this.template_type - 1]].onPagePullDownRefresh();
          })
          this.checkTemplateType()
          uni.stopPullDownRefresh();
     },
     /**
      * 调用组件的下拉加载
      */
     onReachBottom () {
          let template_arr = [
               "define", "diy"
          ];
          if (typeof (this.$refs[template_arr[this.template_type - 1]].onPageReachBottom) == "function") {
               this.$refs[template_arr[this.template_type - 1]].onPageReachBottom();
          }
     },
     methods: {
          async checkTemplateType () {
               // 商户无权限，直接展示默认模板
               if (!this.$store.storediy) {
                    this.template_type = 1;
                    return;
               } else {
                    let newDiyState = await this.getPageData();
                    if (newDiyState) {
                         this.template_type = 2;
                    } else {
                         this.template_type = 1;
                    }
               }

               // 要触发插件里面的onshows方法 
               this.$nextTick(() => {
                    let template_arr = [
                         "define", "diy",
                    ];
                    this.$refs[template_arr[this.template_type - 1]] && this.$refs[template_arr[this.template_type - 1]].onShows();
               })
          },
          // 获取商户页面
          getPageData () {
               return new Promise(async resolve => {
                    try {
                         const {
                              status,
                              data,
                              message,
                         } = await this.$request('DiyStore/Api/getMerchantPageData', {
                              needErrorMsg: false
                         });
                         if (status == 1) {
                              this.page_content = data.content;
                              this.$nextTick(() => {
                                   resolve(true);
                              })
                         } else {
                              resolve(false);
                         }
                    } catch (err) {
                         resolve(false);
                    }
               });
          }
     }

}
</script>
